package com.doit.day03

object _06_算子初步练习 {
  def main(args: Array[String]): Unit = {
    //需求：Array("hello,hello，hi，hello，hi"，"hadoop,hive,hadoop,hive,hadoop","biandan,chige,chige,biandan,chige")
    //Array("hello","hello","hi","hello","hi"，"hadoop","hive","hadoop","hive","hadoop","biandan","chige","chige","biandan","chige")；

    val arr: Array[String] = Array("hello,hello,hi,hello,hi", "hadoop,hive,hadoop,hive,hadoop", "biandan,chige,chige,biandan,chige")
    //晶晶：直接进行flatten   ==》 一个个的字符
    //想想别的啊
    //"hello,hello,hi,hello,hi"
    //"hadoop,hive,hadoop,hive,hadoop"
    /*    for (elem <- arr) {
          val arr1: Array[String] = elem.split(",")
          for (elem <- arr1) {

          }
        }*/

    //map  做映射的  ==》 如果你想要将一个集合中的元素，变成另外一个样子，就直接用map
    //想要将句子里面的单词都拿出来，就需要将句子按照逗号进行切割  map  split(",")==> 数组  Array(Array,Array,Array)  ==>flatten

    val res: Array[String] = arr.map((line: String) => {
      line.split(",")
    }).flatten


    //flatMap  ==》 flatten 和 map   的结合   先做map映射，然后再flatten



    //    groupBy   分组  ==》 没有聚合
    val ints: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1, 2, 1)

    val map: Map[Int, Array[Int]] = ints.groupBy((a: Int) => {
      a
    })

    for (elem <- map) {
      val key: Int = elem._1
      val value: Array[Int] = elem._2
      println(key + "=>" + value.mkString(","))
      /*
1    , Array(1,1,1,1,1)
2    , Array(2,2,2,2)
3    , Array(3,3)
4    , Array(4)
5    , Array(5)
       */
    }


    val orders: Array[ScalaOrder] = Array(
      ScalaOrder("o001", "g001", 9.9, 10),
      ScalaOrder("o001", "g001", 19.9, 29),
      ScalaOrder("o003", "g003", 29.9, 4),
      ScalaOrder("o003", "g003", 92.9, 2),
      ScalaOrder("o003", "g005", 39.9, 1)
    )

    val res33 :Map[String,Array[ScalaOrder]]= orders.groupBy((order: ScalaOrder) => {order.oid})

    for (elem <- res33) {
      println(elem._1+"=>"+elem._2.mkString(","))
    }


    val strings: Array[String] = arr.flatMap((line: String) => {
      line.split(",")
    })

    //Array(hello,hello,hi,hello,hi,hadoop,hive,hadoop,hive,hadoop,biandan,chige,chige,biandan,chige)
    println(strings.mkString(","))
    //需要你统计每一个单词的个数   word count
    val res44 :Map[String,Array[String]] = strings.groupBy((word:String)=>{word})

    /*
    hello ,Array(hello,hello,hello)
    hi    ,Array(hi,hi)
    hadoop,Array(hadoop,hadoop,hadoop)
    hive  ,Array(hive,hive)
    biandan ,Array(biandan,biandan)
    chige   ,Array(chige,chige,biandan)
     */

    val res55: Map[String, Int] = res44.map((tp: (String, Array[String])) => {
      val word: String = tp._1
      //Array(hello,hello,hello)
      val count: Int = tp._2.size
      (word, count)
    })
    res55.foreach(println)


  }

}
